package com.woniuxy.service.indent;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Indent;
import com.woniuxy.entity.User;
import com.woniuxy.mapper.BedMapper;
import com.woniuxy.mapper.IndentMapper;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.service.fault.FaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class IndentServiceImpl implements IndentService {
    @Autowired
    private IndentMapper indentMapper;
    @Autowired
    private FaultService faultService;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public int pay(Indent indent) {
        List<Indent> list = indentMapper.selLastIndent(indent.getUserId());
        Indent indent1 = list.get(0);
        indent1.setIndentStatus("完成");
        indent1.setPayTime(new Date());
        User user = userMapper.selectByPrimaryKey(indent1.getUserId());
        BigDecimal balance = user.getBalance().subtract(indent1.getIndentMoney());
        user.setBalance(balance);
        int i = userMapper.updateByPrimaryKeySelective(user);
        indentMapper.updateByPrimaryKeySelective(indent1);
        return i;
    }

    @Override
    @Transactional
    public int returnBed(Indent indent) {
        List<Indent> list = indentMapper.selLastIndent(indent.getUserId());
        Indent indent1 = list.get(0);
        System.out.println(indent1.toString());
        long startTime = indent1.getIndentTime().getTime();
        int minutes = (int) (startTime / (1000 * 60));
        Date date = new Date();
        long endTime = date.getTime();


        minutes = (int) (endTime / (1000 * 60)) - minutes;
        int time = minutes / 60 + 1;
        BigDecimal money = BigDecimal.valueOf(time * 3);

        indent1.setReturnTime(new Date());
        indent1.setIndentStatus("未付款");
        indent1.setIndentMoney(money);

        indentMapper.updateByPrimaryKeySelective(indent1);
        int rows = bedMapper.returnBedById(indent.getBedId());
        return rows;
    }

    @Override
    public int save(Indent indent) {
        indent.setIndentTime(new Date());
        indent.setIndentStatus("使用中");
        int rows = indentMapper.insertSelective(indent);
        bedMapper.useBedById(indent.getBedId());
        return rows;
    }

    @Override
    public int delete(Integer indentId) {
        int rows = indentMapper.deleteByPrimaryKey(indentId);
        return rows;
    }

    @Override
    public Indent findOne(Integer indentId) {
        Indent indent = indentMapper.selectByPrimaryKey(indentId);
        return indent;
    }

    @Override
    public List<Indent> findAll() {
        List<Indent> list = indentMapper.selectByExample(null);
        return list;

    }

    @Override
    public PageInfo<Indent> findByPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Indent> indents = indentMapper.selectByExample(null);
        PageInfo info = new PageInfo(indents);

        return info;
    }

    @Override
    public int update(Indent indent) {
        int i = indentMapper.updateById(indent.getIndentId());
        faultService.save(indent);
        bedMapper.updateById(indent.getBedId());
        return i;
    }

    @Override
    public List<Indent> findByPage(int page, int size) {
        return null;
    }


    @Override
    public Indent findByFaultId(Integer indentId) {
        return indentMapper.findByFaultId(indentId);
    }

    @Override
    public void updateSuccess(Integer indentId) {
        indentMapper.updateStatusAsSuccess(indentId);
    }
}
